<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2019/2/22
 * Time: 14:15
 */

namespace App\Http\Controllers\Job;


use App\Http\Controllers\BaseApiController;
use App\Services\Job\JobService;
use Illuminate\Http\Request;
use Validator;

class JobinforController extends BaseApiController
{
    private $service;


    public function __construct(JobService $service)
    {
        //继承基类的构造方法
        parent::__construct();
        //依赖注入service
        $this->service=$service;
    }

    /**
     * @param Request $request
     * @return array
     * 职位信息添加接口
     */
    public function JobAdd(Request $request){
        //接受参数
        $data=$request->all();
        $rules=[
            'position_name'=>'required|string|max:255',

            'address'=>'required|string|max:100',

            'high'=>'required',

            'number_recruits'=>'required|int',

            'industry_id'=>'required|int',

        ];
        $message=[
            'position_name.required'=>'请输入职位名称！',

            'address.required'=>'请输入工作地点！',

            'high.required'=>'请输入该职位的薪资范围！',

            'number_recruits.required'=>'请输入招聘人数！',

            'industry_id.required'=>'请选择该职位的行业！',

            'industry_id.int'=>'行业id为int型！',

            'number_recruits.int'=>'招聘人数为int型！'

        ];
        //调用下面验证规则
        $rule = $this -> validator($data,$rules,$message);
        //判断参数是否符合验证规则
        if($rule -> fails()){
            return $rule -> errors();
        }
        //调用service层，将数据传输过去，
        $res=$this->service->addPosition($data);

        return $res;
    }
    /**
     * 职位信息展示
     */
    public function findJob(Request $request){
        //接受公司company_id
        $data['id'] = $request->input('company_id');
        //验证是否接收到id
        $rule = $this->idFind($data);
        if($rule->fails()){
            //不符合返回验证码
            return $rule->errors();
        }

        $res=$this->service->showJob($data['id']);

        return $res;
    }
    /**
     * 职位删除
     */
    public function delJob(Request $request){
        //接收该职位的position_id
        $data['id'] = $request->input('position_id');
        //调用BaseApiController内的验证规则，判断是否接受到id
        $rule = $this->idFind($data);
        if($rule->fails()){
            //不符合返回验证码
            return $rule->errors();
        }
        $res = $this->service->delJob($data['id']);
        return $res;
    }

    /**
     * 职位状态修改
     */
    public function statusShut(Request $request){
        $data['id'] = $request->input('position_id');
        $status = $request->input('status');
        //调用BaseApiController内的验证规则，判断是否接受到id
        $rule = $this->idFind($data);
        if($rule->fails()){
            //不符合返回验证码
            return $rule->errors();
        }
        $res = $this->service->statusShut($data['id'],$status);
        return $res;
    }
    /**
     * 重命名
     */
    public function updName(Request $request){
        $data=$request->input();
        $rules=[
            'position_id'=>'required|int',

            'new_name'=>'required',

        ];
        $message=[
            'position_id.required'=>'要修改的id不能为空！',

            'new_name.required'=>'请输入重命名的名字！'
        ];
        $rule=$this->validator($data,$rules,$message);
        //判断参数是否符合验证规则
        if($rule -> fails()){
            return $rule -> errors();
        }
        //调用service层，将数据传输过去，

        $res=$this->service->updName($data);

        return $res;
    }

    //置顶
    public function roofPlace(Request $request){
        $company_id=$request->input('company_id');
        $position_id=$request->input('position_id');
        $res=$this->service->Top($position_id,$company_id);
        return $res;

    }

}